package client.comm.baoding.ui

import android.content.IntentFilter
import android.view.KeyEvent
import androidx.lifecycle.Observer
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.databinding.ActivityMainBinding
import client.comm.baoding.receiver.MeBroadcast
import client.comm.baoding.ui.vm.MainViewModel
import client.comm.commlib.toast.toast
import java.util.*

class MainActivity : BaseMiddleActivity<MainViewModel, ActivityMainBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_main
    }

//    val meBroadcast: MeBroadcast by lazy {
//        MeBroadcast()
//    }

    override fun onDestroy() {
        super.onDestroy()
//        unregisterReceiver(meBroadcast)
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    val controller: NavController by lazy {
        findNavController(R.id.nav_host_fragment)
    }

    override fun initView() {
//        registerReceiver(meBroadcast, IntentFilter(MeBroadcast.MAIN_SWITCH_TAB))
        controller?.let {
            binding.navView.setupWithNavController(it)
        }

//        controller?.let { binding.navView.setupWithNavController(it) }
//        val navHostFragment = supportFragmentManager
//            .findFragmentById(R.id.nav_host_fragment) as NavHostFragment?
//        val navController = navHostFragment!!.navController
//        NavigationUI.setupActionBarWithNavController(this, navController)
//        NavigationUI.setupWithNavController(binding.navView, navController)
        binding.vm = viewModel

    }

    override fun observerUI() {
        viewModel.switchValue.observe(this, Observer {
            if (it >= 0) {
                when (it) {
//                    3 -> {
//                        controller.navigate(R.id.navigation_me)
//                    }
//                    2 -> {
//                        controller.navigate(R.id.navigation_buycart)
//                    }
//                    0 -> {
//                        controller.navigate(R.id.navigation_home)
//                    }
                }
            }
        })
    }

    fun switchTab(tab: Int) {
        viewModel.switchValue.value = tab
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitLogin()
        }
        return false
    }

    private var isExit = false

    private fun exitLogin() {
        var tExit: Timer? = null
        if (!isExit) {
            isExit = true
            toast("再次返回退出${getString(R.string.app_name)}")
            tExit = Timer()
            tExit.schedule(object : TimerTask() {
                override fun run() {
                    isExit = false //取消退出
                }
            }, 2500) // 如果2.5秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            System.exit(0)
        }
    }


}